package com.qaml.mes.device.domain.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.earth.page.PageResponse;
import com.earth.user.AuthSsoContext;
import com.earth.user.SsoLoginUser;
import com.qaml.mes.device.api.CheckUpkeepMessageService;
import com.qaml.mes.device.dao.CheckUpkeepMessageDO;
import com.qaml.mes.device.repository.CheckUpkeepMessageRepository;
import com.qaml.mes.device.domain.convert.CheckUpkeepMessageConvert;
import com.qaml.mes.device.dto.eqp.CheckUpkeepMessageDTO;
import com.qaml.mes.device.enums.CheckUpkeepMessageTypeEnum;
import com.qaml.mes.device.vo.eqp.CheckUpkeepMessageVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 检保项目消息 服务实现类
 * </p>
 *
 * @author leikai
 * @since 2024-03-04
 */
@Service
public class CheckUpkeepMessageServiceImpl implements CheckUpkeepMessageService {

    /**
     * 点检图标地址
     */
    @Value("${check.logo.url:}")
    private String checkLogoUrl;

    /**
     * 保养图标地址
     */
    @Value("${upkeep.logo.url:}")
    private String upkeepLogoUrl;

    /**
     * 维修图标地址
     */
    @Value("${repair.logo.url:}")
    private String repairLogoUrl;

    @Resource
    private CheckUpkeepMessageRepository checkUpkeepMessageRepository;

    @Override
    public PageResponse<CheckUpkeepMessageVO> pageList(CheckUpkeepMessageDTO checkUpkeepMessageDTO) {
        checkUpkeepMessageDTO.setUserId(AuthSsoContext.getCurrentUserId());
        IPage<CheckUpkeepMessageDO> checkUpkeepMessageDOIPage = checkUpkeepMessageRepository.pageList(checkUpkeepMessageDTO);
        if (checkUpkeepMessageDOIPage == null || CollectionUtils.isEmpty(checkUpkeepMessageDOIPage.getRecords())) {
            return new PageResponse<>(0, null, checkUpkeepMessageDTO);
        }

        List<CheckUpkeepMessageVO> checkUpkeepMessageVOS = CheckUpkeepMessageConvert.INSTANCE.do2ListVo(checkUpkeepMessageDOIPage.getRecords());
        checkUpkeepMessageVOS.forEach(checkUpkeepMessageVO -> {
            if (checkUpkeepMessageVO.getType().equals(CheckUpkeepMessageTypeEnum.CHECK.getType())) {
                checkUpkeepMessageVO.setLogoUrl(checkLogoUrl);
            } else if (checkUpkeepMessageVO.getType().equals(CheckUpkeepMessageTypeEnum.UPKEEP.getType())) {
                checkUpkeepMessageVO.setLogoUrl(upkeepLogoUrl);
            } else if (checkUpkeepMessageVO.getType().equals(CheckUpkeepMessageTypeEnum.REPAIR.getType())) {
                checkUpkeepMessageVO.setLogoUrl(repairLogoUrl);
            }
        });
        return new PageResponse<>(checkUpkeepMessageDOIPage.getTotal(), checkUpkeepMessageVOS, checkUpkeepMessageDTO);
    }

    @Override
    public void updateReadStatus(CheckUpkeepMessageDTO checkUpkeepMessageDTO) {
        CheckUpkeepMessageDO messageDO = checkUpkeepMessageRepository.getById(checkUpkeepMessageDTO.getId());
        if (null == messageDO) {
            return;
        }
        messageDO.setIsRead(1);
        checkUpkeepMessageRepository.updateById(messageDO);
    }

    @Override
    public void readAll() {
        SsoLoginUser ssoLoginUser = AuthSsoContext.getSsoLoginUser();
        String userId = ssoLoginUser.getUserId();
        String userName = ssoLoginUser.getUserName();
        List<CheckUpkeepMessageDO> checkUpkeepMessageDOList = checkUpkeepMessageRepository.getUnReadMessageList(userId);
        if (checkUpkeepMessageDOList.isEmpty()) {
            return;
        }
        List<String> ids = checkUpkeepMessageDOList.stream().map(CheckUpkeepMessageDO::getId).collect(Collectors.toList());
        checkUpkeepMessageRepository.updateByIds(ids, userId, userName);
    }
}
